package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.order.client.OrderInfoFeignClient;
import com.atguigu.tingshu.search.repository.AlbumInfoRepository;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.search.service.MqOpsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Optional;

/**
 * Description:
 *
 * @author Yanxinhao on 2025/1/18
 */

@Service
public class MqOpsServiceImpl implements MqOpsService {

    @Autowired
    private ItemService itemService;

    @Autowired
    private OrderInfoFeignClient orderInfoFeignClient;

    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient;

    @Autowired
    private AlbumInfoRepository albumInfoRepository;


    /**
     * 根据拿到的消息（专辑id）
     * 上传指定专辑到es库中
     * @param content 专辑id
     */
    @Override
    public void listenAlbumUpper(String content) {
        itemService.upperAlbum(Long.parseLong(content));
    }


    /**
     * 下架
     * @param content 专辑id
     */
    @Override
    public void listenAlbumLower(String content) {
        itemService.lowerAlbum(Long.parseLong(content));
    }


    /**
     * 修改es中专辑的购买量信息
     * @param orderNo 订单号
     */
    @Override
    public void listenAlbumStatUpdate(String orderNo) {
        //1.根据订单号,获取订单对象,我们需要OrderInfo中的订单详情数据,比如消费类型
        Result<OrderInfo> orderInfoResult = orderInfoFeignClient.getOrderInfoByOrderNo(orderNo);
        OrderInfo orderInfo = orderInfoResult.getData();
        Assert.notNull(orderInfo,"[MqOpsServiceImpl]调用order微服务的[getOrderInfoByOrderNo]方法出错");

        //获取购买量
        int orderSize = orderInfo.getOrderDetailList().size();
        //获取购买类型 --1001专辑  --1002音频
        String itemType = orderInfo.getItemType();

        Long albumId = null;
        if ("1001".equals(itemType)){
            //专辑,那么我们去修改专辑的购买量,传来的订单详情中就包含了专辑id
            albumId = orderInfo.getOrderDetailList().get(0).getItemId();
        } else if ("1002".equals(itemType)){
            //音频,那么我们需要去修改专辑的购买量,传来的订单中只有音频id,需要根据音频id获取音频对象在获取专辑id
            Long trackId = orderInfo.getOrderDetailList().get(0).getItemId();
            Result<TrackInfo> trackInfoResult = trackInfoFeignClient.getTrackInfoById(trackId);
            TrackInfo trackInfo = trackInfoResult.getData();
            Assert.notNull(trackInfo,"[MqOpsServiceImpl]调用track微服务的[getTrackInfoById]方法出错");
            albumId = trackInfo.getAlbumId();

        } else {
            //买的是vip,无需修改es中专辑的购买量
        }

        //现在获取到了专辑id,去根据专辑id获取es中具体的专辑数据对象,修改它的购买量
        Optional<AlbumInfoIndex> albumInfoIndex = albumInfoRepository.findById(albumId);
        albumInfoIndex.ifPresent(index -> {
            Integer buyStatNum = index.getBuyStatNum();
            buyStatNum = buyStatNum + orderSize;
            index.setBuyStatNum(buyStatNum);
            albumInfoRepository.save(index);
        });

    }
}